package com.fr.ticket;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author 付荣
 * @version 创建时间：2019年4月11日 下午1:49:02
 */
public class MtSimilarity {
	// private static final String dic_file = "E:\\ticket\\dic.txt";
	private static final String product_relation = "E:\\ticket\\mt_product_relation2.txt";
	private static final String mt_product = "E:\\ticket\\mt_clean2.txt";

	private static final String[] stopWords = { "景区", "风景区", "公园", "乐园", "度假区" };

	public static void main(String[] args) throws IOException {
		Set<String> set = new HashSet<>();
		for(String stopword : stopWords){
			set.add(stopword);
		}
		TransportClient client = ESUtils.getClient();
		SearchRequestBuilder sr = client.prepareSearch("product");
		sr.setSize(4);
		// FileWriter dicWriter = new FileWriter(dic_file);
		FileWriter productRelationWriter = new FileWriter(product_relation);
		FileReader fr = new FileReader(mt_product);
		BufferedReader br = new BufferedReader(fr);
		String line = br.readLine();
		while (line != null) {
			String[] s = line.split("\t");
			if (s.length >= 5) {
				String id = s[1];
				String name = s[2];
				String provinceName = s[0];
				List<String> tokens = LvmamaDataUtil.getAnalyzer(name);
				if (CollectionUtils.isNotEmpty(tokens)) {
					BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
					FunctionScoreQueryBuilder functionScoreQuery = QueryBuilders.functionScoreQuery(boolQueryBuilder);
					boolQueryBuilder.must(QueryBuilders.termQuery("provinceName", provinceName));
					boolQueryBuilder.must(QueryBuilders.termsQuery("categoryId", "11", "12"));
					// boolean failFirst = true;
					int count = 0;
					for (int i = 0; i < tokens.size(); i++) {
						String token = tokens.get(i);
						if (token.length() < 2) {
							// if(failFirst){
							// failFirst = false;
							// dicWriter.append("分词结果异常:" + name + ":" + tokens
							// + "\n");
							// }
							count++;
							boolQueryBuilder.should(QueryBuilders.wildcardQuery("productName", "*" + token + "*").queryName("name_" + i));
							functionScoreQuery.add(QueryBuilders.wildcardQuery("productName", "*" + token + "*"), ScoreFunctionBuilders.weightFactorFunction(3));
						} else {
							if(!set.contains(token)){
								boolQueryBuilder.should(QueryBuilders.termQuery("productName", token).queryName("name_" + i));
								functionScoreQuery.add(QueryBuilders.termQuery("productName", token), ScoreFunctionBuilders.weightFactorFunction(6));
								count++;
							}
						}
					}
					if (count >= 5) {
						boolQueryBuilder.minimumNumberShouldMatch(3);
					} else if (count >= 3) {
						boolQueryBuilder.minimumNumberShouldMatch(2);
					} else {
						boolQueryBuilder.minimumNumberShouldMatch(1);
					}
					sr.setQuery(functionScoreQuery);
					String string = LvmamaDataUtil.getTicketProductOrGoods(sr.toString(), 1);
					JSONObject object = JSON.parseObject(string);
					List<JSONObject> ticketProduct = (List<JSONObject>) ((JSONObject) object.get("hits")).get("hits");
					if (CollectionUtils.isNotEmpty(ticketProduct)) {
						if (ticketProduct.size() > 1) {
							boolean f = true;
							for (JSONObject jo : ticketProduct) {
								JSONObject j = jo.getJSONObject("_source");
								String lvmamaProductName = String.valueOf(j.get("productName"));
								lvmamaProductName = lvmamaProductName.replaceAll("\n", "");
								String lvmamaAddress = String.valueOf(j.get("address"));
								lvmamaAddress = lvmamaAddress.replaceAll("\n", "");
								if (f) {
									productRelationWriter.append(2 + "\t" + s[0] + "\t" + id + "\t" + name + "\t" + s[3] + "\t" + s[4] + "\t" + j.get("productId") + "\t" + lvmamaProductName + "\t" + lvmamaAddress + "\t" + j.get("sellPrice") + "\t其他可能结果:");
									f = false;
								} else {
									productRelationWriter.append("\t" + j.get("productId") + "\t" + lvmamaProductName + "\t" + lvmamaAddress + "\t" + j.get("sellPrice") + "\t");
								}
							}
							productRelationWriter.append("\n");
						} else {
							JSONObject first = ticketProduct.get(0);
							JSONObject j = first.getJSONObject("_source");
							String lvmamaProductName = String.valueOf(j.get("productName"));
							lvmamaProductName = lvmamaProductName.replaceAll("\n", "");
							String lvmamaAddress = String.valueOf(j.get("address"));
							lvmamaAddress = lvmamaAddress.replaceAll("\n", "");
							productRelationWriter.append(1 + "\t" + s[0] + "\t" + id + "\t" + name + "\t" + s[3] + "\t" + s[4] + "\t" + j.get("productId") + "\t" + lvmamaProductName + "\t" + lvmamaAddress + "\t" + j.get("sellPrice") + "\n");
						}
					} else {
						productRelationWriter.append(3 + "\t" + s[0] + "\t" + id + "\t" + name + "\t" + s[3] + "\t" + s[4] + "\t" + "无匹配结果" + "\n");
					}
				}
			}
			line = br.readLine();
		}

		// dicWriter.close();
		productRelationWriter.close();
		br.close();
		fr.close();
	}

}
